﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace SQLServerDAL
{
    public class CardTypeDAL : ICardTypeDAL 
    {
        public Boolean InsertCardType(CardType cardType)
        {
            try
            {

                CardTypeDataClassesDataContext cardTypeContext = new CardTypeDataClassesDataContext();
                cardTypeContext.CardTypes.InsertOnSubmit(cardType);
                cardTypeContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean DeleteCardType(int cardTypeId)
        {
            try
            {
                CardTypeDataClassesDataContext cardTypeContext = new CardTypeDataClassesDataContext();
                CardType cardType = (from u in cardTypeContext.CardTypes
                             where u.CardTypeId.Equals(cardTypeId)
                             select u).First();
                cardTypeContext.CardTypes.DeleteOnSubmit(cardType);
                cardTypeContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }


        public Boolean UpdateCardType(int cardTypeId, CardType cardType)
        {

            if (cardTypeId != cardType.CardTypeId)
            {
                return false;
            }
            CardTypeDataClassesDataContext cardTypeContext = new CardTypeDataClassesDataContext();
            CardType query = (from u in cardTypeContext.CardTypes
                          where u.CardTypeId.Equals(cardTypeId)
                          select u).First();
            //query.Password = cardType.Password;

            cardTypeContext.SubmitChanges();
            return true;

        }

        public CardType SelectCardTypeById(int cardTypeId)
        {
            CardTypeDataClassesDataContext cardTypeContext = new CardTypeDataClassesDataContext();
            CardType cardType = (from u in cardTypeContext.CardTypes
                         where u.CardTypeId.Equals(cardTypeId)
                         select u).First();
            return cardType;
        }


        public List<CardType> SelectAllCardType()
        {
            CardTypeDataClassesDataContext cardTypeContext = new CardTypeDataClassesDataContext();
            var query = (from u in cardTypeContext.CardTypes
                         select u);
            List<CardType> cardTypeList = new List<CardType>();

            foreach (var cardType in query)
            {
                cardTypeList.Add(cardType);
            }
            return cardTypeList;
        }
    }
}
